let express = require('express');
let router = express.Router();
let order = require('../models/order');

// 该路由使用的中间件
router.use(function timeLog(req, res, next) {
	next();
});
function jsonData(code,d) {
	return ( {error_code:code,data:d });
}
function formatDateTime(timestamp){
	let date = new Date(timestamp);
	let Y = date.getFullYear() + '-';
	let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
	let D = date.getDate() + ' ';
	let h = date.getHours() + ':';
	let m = date.getMinutes() + ':';
	let s = date.getSeconds();
	return Y+M+D+h+m+s;
}
/*路由控制*/
router.all('/create', (req, res, next)=> {
	res.header("Access-Control-Allow-Origin", "*");
	res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
	res.header("Access-Control-Allow-Headers", "X-Requested-With");
	res.header('Access-Control-Allow-Headers', 'Content-Type');
	console.log('req',req.body);
	let parsePostBody =  (req, done)=> {
		let length = req.headers['content-length'] - 0;
		let arr = [];
		let chunks;

		req.on('data', buff => {
			arr.push(buff);
		});

		req.on('end', () => {
			chunks = Buffer.concat(arr);
			done(chunks);
		});
	};

	let _submit = function (json) {
		if(!json.p_id){
			res.json(jsonData(-1,{}));
		}
		let data = {
			p_id:json.p_id,
			p_title:json.p_title,
			p_cover:json.p_cover,
			o_price:json.p_price*json.o_copies,
			o_copies:json.o_copies,
			create_date:new Date().getTime()
		};
		order.create(data,function (d) {
			res.json(jsonData(0,d));
		});
	};

	if(req.body){
		let json =req.body;
		_submit(json);
	}else{
		parsePostBody(req, (chunks) => {
			let json = JSON.parse( chunks.toString() );    // 关键代码
			_submit(json);
		});
	}
});

router.get('/list', (req, res, next)=> {
	res.header("Access-Control-Allow-Origin", "*");
	res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
	res.header("Access-Control-Allow-Headers", "X-Requested-With");
	res.header('Access-Control-Allow-Headers', 'Content-Type');
	let json = {orderBy:'DESC'};
	order.list(json,function (d) {
		d.map((v,k)=>{
			d[k].create_date = formatDateTime(d[k].create_date);
		});
		res.json(jsonData(0,d));
	});
});

router.get('/detail', (req, res, next)=> {
	res.header("Access-Control-Allow-Origin", "*");
	res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
	res.header("Access-Control-Allow-Headers", "X-Requested-With");
	res.header('Access-Control-Allow-Headers', 'Content-Type');
	let id = req.query.id;
	order.detail(id,function (d) {
		d[0].create_date = formatDateTime(d[0].create_date);
		res.json(jsonData(0,d[0]));
	});
});

router.get('/total', (req, res, next)=> {
	res.header("Access-Control-Allow-Origin", "*");
	res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
	res.header("Access-Control-Allow-Headers", "X-Requested-With");
	res.header('Access-Control-Allow-Headers', 'Content-Type');
	order.total(function (d) {
		res.json(jsonData(0,d[0]));
	});
});

module.exports = router;
